package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * 描述：
 * 作者 张超
 * 时间 2018/10/29
 * 项目 springboot-demo v1.0
 * 版本 v1.0
 */
@Configuration
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //允许跨域允
        // 许客户端采用socket
        //路径"/stomp"被注册为STOMP端点，对外暴露，客户端通过该路径接入WebSocket服务
        registry.addEndpoint("/stomp").setAllowedOrigins("*").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //订阅的前缀
        registry.enableSimpleBroker("/queue", "/topic");
        //全局使用的订阅前缀（相当于工程名）客户端send时前缀；@MessageMapping时用到  "app/@MessageMapping("msg")"
        registry.setApplicationDestinationPrefixes("/app");
//点对点使用的订阅前缀（客户端订阅路径上会体现出来），不设置的话，默认也是/user/
        registry.setUserDestinationPrefix("/user/");
    }


}
